<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .imgContainer {
        width: 400px;
        height: 400px;
        background-color: gray;
      }
    </style>
  </head>
  <body>
    <div class="imgContainer"></div>
    <button>按钮</button>
  </body>
</html>
<script>
  var imgArr = ["./img/9.jpg", "./img/10.jpg", "./img/15.jpg"];
  //   console.log(imgarr);
  function randomNum(min, max) {
    return parseInt(Math.random() * (max + 1 - min)) + min;
  }
  //   console.log(randomNum(0, 3));
  var btn = document.querySelector("button");
  var imgContainerEle = document.querySelector(".imgContainer");
  var chiosekey = -1;
  btn.onclick = function () {
    var key = randomNum(0, imgArr.length - 1);
    while (chiosekey == key) {
      key = randomNum(0, imgArr.length - 1);
    }
    chiosekey = key;
    console.log(key);
    imgContainerEle.style.backgroundImage = `url(${imgArr[key]})`;
  };
</script>
